﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;
using System.Data.SqlClient;

namespace HATX_Provision
{
    public partial class ProvisionUltils : Form
    {
        string[] arrDanhMuc = new string[] { "DmTuyen", "PhanTram", "DkThuong", "DmGiaXang", "DmGara", "DmLoaiHd", "DmLoaiGt", "DmCx", "DmNhKh", "DmKh", "KhenThuongLx", "TBTuyen", "PsNo", "TraNo", "NhienLieu", "LxViPham", "ChamCongLx" };
        string[] arrPhieu = new string[] { "Dmxe", "Dmkx", "DmBp", "DmKmCn", "NghiemThu" };

        public ProvisionUltils()
        {
            InitializeComponent();
        }

        private void btnProvision_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn1 = new SqlConnection(txtConStr1.Text))
            {
                using (SqlConnection conn2 = new SqlConnection(txtConStr2.Text))
                {
                    foreach (string node in arrDanhMuc)
                    {

                        DbSyncScopeDescription nodeDes = new DbSyncScopeDescription(node + "Scope");
                        DbSyncTableDescription tableDes = SqlSyncDescriptionBuilder.GetDescriptionForTable(node, conn1);
                        nodeDes.Tables.Add(tableDes);
                        SqlSyncScopeProvisioning provi = new SqlSyncScopeProvisioning(conn2, nodeDes);
                        if (provi.ScopeExists(nodeDes.ScopeName))
                        {
                            Console.WriteLine("Skip for exists==>" + nodeDes.ScopeName);
                            return;
                        }
                        provi.ObjectSchema = "Sync";
                        provi.SetUseBulkProceduresDefault(false);


                        Console.WriteLine(nodeDes.ScopeName);
                        provi.Apply();
                    }
                    foreach (string node in arrPhieu)
                    {

                        DbSyncScopeDescription nodeDes = new DbSyncScopeDescription(node + "Scope");
                        DbSyncTableDescription tableDes = SqlSyncDescriptionBuilder.GetDescriptionForTable(node, conn1);
                        nodeDes.Tables.Add(tableDes);
                        SqlSyncScopeProvisioning provi = new SqlSyncScopeProvisioning(conn2, nodeDes);
                        if (provi.ScopeExists(nodeDes.ScopeName))
                        {
                            Console.WriteLine("Skip for exists==>" + nodeDes.ScopeName);
                            return;
                        }
                        provi.ObjectSchema = "Sync";
                        provi.SetUseBulkProceduresDefault(false);

                        Console.WriteLine(nodeDes.ScopeName);
                        provi.Apply();
                    }
                    Console.WriteLine("Finish all");
                }
            }
        }

        private void btnConn1_Click(object sender, EventArgs e)
        {
            DataConnectionDialog dcd = new DataConnectionDialog();
            //DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
            //dcs.LoadConfiguration(dcd);
            DataSource ds = new DataSource("MicrosoftSqlServer", "Microsoft SQL Server");
            dcd.DataSources.Add(ds);
            ds.Providers.Add(DataProvider.SqlDataProvider);
            dcd.SelectedDataProvider = DataProvider.SqlDataProvider;
            dcd.SelectedDataSource = ds;
            dcd.ConnectionString = txtConStr1.Text;
            if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
            {
                txtConStr1.Text = dcd.ConnectionString;
                config.IniWriteValue("ConnectionString", "TemplateDb",txtConStr1.Text);
              
            }
        }


        private void btnCon2_Click(object sender, EventArgs e)
        {
            DataConnectionDialog dcd = new DataConnectionDialog();
            //DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
            //dcs.LoadConfiguration(dcd);
            DataSource ds = new DataSource("MicrosoftSqlServer", "Microsoft SQL Server");
            ds.Providers.Add(DataProvider.SqlDataProvider);
            dcd.DataSources.Add(ds);
            dcd.SelectedDataProvider = DataProvider.SqlDataProvider;
            dcd.SelectedDataSource = ds;
            dcd.ConnectionString = txtConStr2.Text;
            if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
            {
                txtConStr2.Text = dcd.ConnectionString;
                
                config.IniWriteValue("ConnectionString", "SyncDb",txtConStr2.Text);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnDeProvision_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn2 = new SqlConnection(txtConStr2.Text))
            {

                SqlSyncScopeDeprovisioning depro = new SqlSyncScopeDeprovisioning(conn2);

                //depro.DeprovisionStore();
                depro.ObjectSchema = "Sync";

                SqlSyncScopeProvisioning severProvi0 = new SqlSyncScopeProvisioning(conn2);
                severProvi0.ObjectSchema = "Sync";
                foreach (string node in arrDanhMuc)
                {

                    if (severProvi0.ScopeExists(node + "Scope"))
                    {
                        Console.WriteLine("DeprovisionScope:" + node + "Scope");
                        depro.DeprovisionScope(node + "Scope");

                    }
                }
                foreach (string node in arrPhieu)
                {

                    if (severProvi0.ScopeExists(node + "Scope"))
                    {
                        Console.WriteLine("DeprovisionScope:" + node + "Scope");
                        depro.DeprovisionScope(node + "Scope");
                    }
                }

            }
        }
        IniHelper config = new IniHelper(Application.StartupPath + "\\Config.ini");
        private void ProvisionUltils_Load(object sender, EventArgs e)
        {
            txtConStr1.Text = config.IniReadValue("ConnectionString", "TemplateDb");
            txtConStr2.Text = config.IniReadValue("ConnectionString", "SyncDb");

        }


    }
}
